package com.gt.login.activities;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.gt.login.Constants;
import com.gt.login.ListUserDetailArrayAdapter;
import com.gt.login.R;
import com.gt.login.SaxParserUser;
import com.gt.login.Session;
import com.gt.login.User;

public class FileListActivity extends ListActivity {

	final String OP_DATA_FOLDER = Constants.OP_DATA_PATH;
	int requestCode;
	List<User> allUsersFromFile;
	boolean isLoggedIn = false;
	private static int INTENT_LOGIN = 0;
	private static int INTENT_NEW_ENTRY = 1;
	private static int INTENT_EDIT_ENTRY = 2;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		loginBeforeStart();

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (isLoggedIn) {
			showFiles();
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == INTENT_LOGIN) {
			if (resultCode == RESULT_OK) {
				System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
				isLoggedIn = true;
			
			} else {
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}
	}

	@Override
	public void onBackPressed() {
		// android.os.Process.killProcess(android.os.Process.myPid());
	}

	private String getXmlFileOfSelectedItem(String selectedFile) {
		return OP_DATA_FOLDER + selectedFile;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.browser_menu, menu);
		return true;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		User user=(User)this.getListAdapter().getItem(position);
		String selFile = user.getFileName();
		SaxParserUser sp = new SaxParserUser(
				getXmlFileOfSelectedItem(selFile));
		user = sp.parseFileAndGetListOfUsers().get(0);
		showUserDetail(user);
		System.out.println(user.toString());
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.backMnu:
			logout();
			return true;
		case R.id.closeMnu:
			android.os.Process.killProcess(android.os.Process.myPid());
			return true;
		case R.id.newItem:
			changeToUserDetailEntry();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showFiles() {
		File[] files = new File(OP_DATA_FOLDER).listFiles();
		allUsersFromFile = new ArrayList<User>();
		for (File file : files) {
			SaxParserUser sp = new SaxParserUser(OP_DATA_FOLDER
					+ file.getName());
			User tmp = sp.parseFileAndGetListOfUsers().get(0);
			// list values are in User object
			tmp.setFileName(file.getName());
			System.out.println(file.getName());
			String[] spl = file.getName().split("_");
			tmp.setCreatedDate(spl[0]);
			tmp.setCreatedTime(spl[1]);
			allUsersFromFile.add(tmp);

		}
		setListAdapter( new ListUserDetailArrayAdapter(this, R.layout.list_layout, allUsersFromFile));
	}

	private void loginBeforeStart() {
		Session ss = ((Session) getApplicationContext());
		if (!ss.isUserLoggedIn()) {
			Intent intent = new Intent(this, LoginPage.class);
			startActivityForResult(intent, INTENT_LOGIN);
		}
	}

	private void logout() {
		Session ss = ((Session) getApplicationContext());
		ss.logOut();
		Intent intent = new Intent(this, LoginPage.class);
		startActivityForResult(intent, INTENT_LOGIN);
	}

	private void changeToUserDetailEntry() {
		Intent i = new Intent(this, UserDetailViewEdit.class);
		i.putExtra("ISENTRY", true);
		startActivityForResult(i, INTENT_NEW_ENTRY);

	}

	private void showUserDetail(User user) {
		Intent i = new Intent(this, UserDetailViewEdit.class);
		i.putExtra("ISENTRY", false);
		i.putExtra("USER", user);
		startActivityForResult(i, INTENT_EDIT_ENTRY);
	}

	


}